

#include "myuart.h"
#include "cleaner.h"

extern cleaner* CleanerCar;

uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_APB,
    };


void uart_init(void){
    uart_driver_install(UART_PORT, BUF_SIZE * 2, 0, 0, NULL, 0);
    uart_param_config(UART_PORT, &uart_config);
    uart_set_pin(UART_PORT, UART_PORT_TXD, UART_PORT_RXD, UART_PORT_RTS, UART_PORT_CTS);

}

void set_uart_pack( const uint8_t * receivebuffer, int len, uint8_t* data){
    data[0] = 0xAF;
    int i = 1;
    for (i = 1; i < len+1; i++)
    {
        data[i] = receivebuffer[i-1];
    }
    data[i] = 0xFA;
}


extern QueueHandle_t xQueueMqtt2Uart;
extern QueueHandle_t xQueueUart2Mqtt;



static void init_config_starttime(void){
    uint8_t send_data[3] = {0};
    uint8_t receive_data[3] = {0};
    uint8_t hour = 0;
    uint8_t min = 0;
    //设置开始时间
    hour = CleanerCar->Work.TargetStartTime/100;
    min = CleanerCar->Work.TargetStartTime%100;
    send_data[0] = UART_SET_TIME;
    send_data[1] = hour;
    send_data[2] = min;
    xQueueSend( xQueueMqtt2Uart, /* 消息队列句柄 */
                (const void*)send_data, /* 要传递数据地址 */
                portMAX_DELAY /* 等待消息队列有空间的最大等待时间 */
              );
    xQueueReceive(  xQueueUart2Mqtt, /* 消息队列句柄 */
                    receive_data, /* 接收消息队列数据的缓冲地址 */
                    portMAX_DELAY /* 等待消息队列有数据的最大等待时间 */
                    );
    CleanerCar->Work.CarStartTime = receive_data[1]*100 + receive_data[2];
}

static void init_get_batval(void){
    uint8_t send_data[3] = {0};
    uint8_t receive_data[3] = {0};
    send_data[0] = UART_GET_BATVAL;
    send_data[1] = 0;
    send_data[2] = 0;
    xQueueSend( xQueueMqtt2Uart, /* 消息队列句柄 */
                (const void*)send_data, /* 要传递数据地址 */
                portMAX_DELAY /* 等待消息队列有空间的最大等待时间 */
              );
    xQueueReceive(  xQueueUart2Mqtt, /* 消息队列句柄 */
                    receive_data, /* 接收消息队列数据的缓冲地址 */
                    portMAX_DELAY /* 等待消息队列有数据的最大等待时间 */
                    );
    CleanerCar->Battery.CurrentVal = receive_data[1];
}

static void get_workstate(void){
    uint8_t send_data[3] = {0};
    uint8_t receive_data[3] = {0};
    send_data[0] = UART_GET_STATE;
    send_data[1] = 0;
    send_data[2] = 0;
    xQueueSend( xQueueMqtt2Uart, /* 消息队列句柄 */
                (const void*)send_data, /* 要传递数据地址 */
                portMAX_DELAY /* 等待消息队列有空间的最大等待时间 */
              );
    xQueueReceive(  xQueueUart2Mqtt, /* 消息队列句柄 */
                    receive_data, /* 接收消息队列数据的缓冲地址 */
                    portMAX_DELAY /* 等待消息队列有数据的最大等待时间 */
                    );
    CleanerCar->Work.WorkState = receive_data[1]; 
}

static void get_charge_state(void){
    //没实现充电功能，，默认不需要充电
    CleanerCar->Battery.ChargeState = CHARGED;
}

void uart_config_car(void){
    //设置开始时间
    init_config_starttime();
    //获取电池电量
    init_get_batval();
    //获取工作状态 
    get_workstate();
    //获取充电状态
    get_charge_state();
}

